//
//  MainTabbar.swift
//  sina
//
//  Created by 江先铨 on 15/11/10.
//  Copyright © 2015年 jxqstatic. All rights reserved.
//

import UIKit

class MainTabbar: UITabBar {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
       // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setUI()
    }
    
    
    func setUI() {
    
      addSubview(compaseBtn)
        
    }
    

    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = self.bounds.width / 5
        
        let h = self.bounds.height
        
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        var index: CGFloat = 0
        
        for sub in subviews {
        
            if sub .isKindOfClass(NSClassFromString("UITabBarButton")!) {
            
              sub.frame = CGRectOffset(rect, index * w , 0)
                
                index += index == 1 ? 2 : 1
            }
           // print(sub)
        }
        
        compaseBtn.frame = CGRectOffset(rect, w * 2, -20)  //让按钮偏移2 个宽度 到达指定的位置
        
        bringSubviewToFront(compaseBtn) //将按钮前置
    }
    
    
    lazy var compaseBtn: UIButton = { //懒加载一个uibutton  他的返回值为button
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        
       btn.sizeToFit()
        
        return btn
        
        }()

}

 